package com.xiaren.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

/**
 * @Description redis相关操作
 * @Author xiaren
 * @Date 18/12/16 11:47
 **/
@Slf4j
@Service
public class RedisService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * redis相关测试
     */
    @Transactional
    public void demoRedis(){
        redisTemplate.setEnableTransactionSupport(true);    // 开启事务
        String random = UUID.randomUUID().toString();
        log.info("更改后的数据-->{}", random);
        redisTemplate.opsForValue().set("aaa", random);
        System.out.println(1/0);
    }

    /**
     * 测试不要spring注解标志，自己手动多个事务尝试
     */
    public void demoRedis2(){
        redisTemplate.setEnableTransactionSupport(true);    // 开启事务
        redisTemplate.opsForValue().set("aaa", "100");
        System.out.println(1/0);
        redisTemplate.opsForValue().set("aaa", "200");
    }

    /**
     * 测试不要spring注解标志，自己手动多个事务尝试(加注解的情况)
     */
    @Transactional
    public void demoRedis3(){
        redisTemplate.setEnableTransactionSupport(true);    // 开启事务
        redisTemplate.opsForValue().set("aaa", "100");
        System.out.println(1/0);
        redisTemplate.opsForValue().set("aaa", "200");
    }

    @Transactional
    public void demoRedis4(){
        redisTemplate.setEnableTransactionSupport(false);    // 开启事务
        redisTemplate.opsForValue().set("aaa", "100");
        System.out.println(1/0);
        redisTemplate.opsForValue().set("aaa", "200");
    }


}
